/*
 * ========================
 * Author : David Albertson
 * Date : October 5, 2011
 * Project : andro-repo
 * ========================
 */

package com.andengine.lwtemplate;

import android.R;

import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.service.wallpaper.WallpaperService;
import android.text.format.Time;
import android.util.Log;
import android.view.SurfaceHolder;

public class LiveWallpaper extends WallpaperService implements SharedPreferences.OnSharedPreferenceChangeListener
{
	// ===========================================================
	// Constants
	// ===========================================================
	
	public static final String SHARED_PREFS_NAME = "livewallpapertemplatesettings";

	//Camera Constants
	// ASUS TRANFORMER : 1280 x 800
	private static final int CAMERA_WIDTH = 1280;
	private static final int CAMERA_HEIGHT = 800;
	private Resources res;


	// ===========================================================
	// Fields
	// ===========================================================
	
	//Shared Preferences
	private SharedPreferences mSharedPreferences;

	// ===========================================================
	// Constructors
	// ===========================================================

	// ===========================================================
	// Getter & Setter
	// ===========================================================

	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================
/*
	@Override
	public org.anddev.andengine.engine.Engine onLoadEngine()
	{
		Camera mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		return new org.anddev.andengine.engine.Engine(new EngineOptions(true, ScreenOrientation.PORTRAIT, new FillResolutionPolicy(), mCamera));
	}
*/
	@Override
	public Engine onCreateEngine()
	{
		res = this.getResources();
		return new WEngine();
	}

	class WEngine extends Engine implements
			SharedPreferences.OnSharedPreferenceChangeListener
	{
		private final Handler		mHandler		= new Handler();
		private boolean				mVisible;
		private int i;
		private MainThread thread;
		private int pos;
		
		WEngine()
		{
			
			
			
		}
		
		public void onSharedPreferenceChanged(SharedPreferences prefs,
				String key)
		{
			
		}
		
		public void onCreate(SurfaceHolder surfaceHolder)
		{
			Log.d("LOL", "onCreate");
			super.onCreate(surfaceHolder);
			setTouchEventsEnabled(true);
			i = 0;
			thread = new MainThread(surfaceHolder, this);
			thread.setRunning();
			thread.start();
		}
		
		@Override
		public void onDestroy()
		{
			super.onDestroy();
			//mHandler.removeCallbacks(mDrawPattern);
		}

		@Override
		public void onVisibilityChanged(boolean visible)
		{
			mVisible = visible;
			if (visible)
			{
				//drawFrame();
			}
			else
			{
				//mHandler.removeCallbacks(mDrawPattern);
			}
		}
		
		@Override
		public void onSurfaceChanged(SurfaceHolder holder, int format,
				int width, int height)
		{
			super.onSurfaceChanged(holder, format, width, height);

			//initFrameParams();

			//drawFrame();
		}

		@Override
		public void onSurfaceCreated(SurfaceHolder holder)
		{
			super.onSurfaceCreated(holder);
		}

		@Override
		public void onSurfaceDestroyed(SurfaceHolder holder)
		{
			super.onSurfaceDestroyed(holder);
			mVisible = false;
		//	mHandler.removeCallbacks(mDrawPattern);
		}

		@Override
		public void onOffsetsChanged(float xOffset, float yOffset, float xStep,
				float yStep, int xPixels, int yPixels)
		{

			//drawFrame();
		}
		
		void drawFrame(Canvas c)
		{
		
				if (c != null)
				{
					// draw something
					//drawPattern(c);
					//drawTouchPoint(c);
					Paint p = new Paint();
					p.setColor(Color.BLACK);
					c.drawRect(0, 0, 1280, 800, p);
					
					
					printTime(c);
				}
		
			//mHandler.removeCallbacks(mDrawPattern);
			if (mVisible)
			{
				//mHandler.postDelayed(mDrawPattern, 1000 / 25);
			}
			
		}
		
		public void printTime(Canvas c){
			float t = System.currentTimeMillis();
			Time time = new Time();
			
			time.setToNow();
			
			String timz = time.format("%S%M%H");
			
			Paint paint = new Paint();
			paint.setColor(Color.RED);
			paint.setTextSize(20);
			
			pos = 20;
			
			switch(timz.charAt(1)){
				
				case '0':
					c.drawBitmap(BitmapFactory.decodeResource(res, 0x7f020000),  pos + 45 * 5 + 15 * 2, 200, null);
					break;
				case '1':
					c.drawBitmap(BitmapFactory.decodeResource(res, 0x7f020001),  pos + 45 * 5 + 15 * 2, 200, null);
					break;
				case '2':
					c.drawBitmap(BitmapFactory.decodeResource(res, 0x7f020002),  pos + 45 * 5 + 15 * 2, 200, null);
					break;
				case '3':
					c.drawBitmap(BitmapFactory.decodeResource(res, 0x7f020003),  pos + 45 * 5 + 15 * 2, 200, null);
					break;
				case '4':
					c.drawBitmap(BitmapFactory.decodeResource(res, 0x7f020004),  pos + 45 * 5 + 15 * 2, 200, null);
					break;
				case '5':
					c.drawBitmap(BitmapFactory.decodeResource(res, 0x7f020005),  pos + 45 * 5 + 15 * 2, 200, null);
					break;
				case '6':
					c.drawBitmap(BitmapFactory.decodeResource(res, 0x7f020006),  pos + 45 * 5 + 15 * 2, 200, null);
					break;
				case '7':
					c.drawBitmap(BitmapFactory.decodeResource(res, 0x7f020007),  pos + 45 * 5 + 15 * 2, 200, null);
					break;
				case '8':
					c.drawBitmap(BitmapFactory.decodeResource(res, 0x7f020008),  pos + 45 * 5 + 15 * 2, 200, null);
					break;
				case '9':
					c.drawBitmap(BitmapFactory.decodeResource(res, 0x7f020009),  pos + 45 * 5 + 15 * 2, 200, null);
					break;
					
			}
			
			switch(timz.charAt(0)){
			case '0':
				c.drawBitmap(BitmapFactory.decodeResource(res, 0x7f020000),  pos + 45 * 4 + 15 * 2, 200, null);
				break;
			case '1':
				c.drawBitmap(BitmapFactory.decodeResource(res, 0x7f020001),  pos + 45 * 4 + 15 * 2, 200, null);
				break;
			case '2':
				c.drawBitmap(BitmapFactory.decodeResource(res, 0x7f020002),  pos + 45 * 4 + 15 * 2, 200, null);
				break;
			case '3':
				c.drawBitmap(BitmapFactory.decodeResource(res, 0x7f020003),  pos + 45 * 4 + 15 * 2, 200, null);
				break;
			case '4':
				c.drawBitmap(BitmapFactory.decodeResource(res, 0x7f020004),  pos + 45 * 4 + 15 * 2, 200, null);
				break;
			case '5':
				c.drawBitmap(BitmapFactory.decodeResource(res, 0x7f020005),  pos + 45 * 4 + 15 * 2, 200, null);
				break;
			case '6':
				c.drawBitmap(BitmapFactory.decodeResource(res, 0x7f020006),  pos + 45 * 4 + 15 * 2, 200, null);
				break;
			case '7':
				c.drawBitmap(BitmapFactory.decodeResource(res, 0x7f020007),  pos + 45 * 4 + 15 * 2, 200, null);
				break;
			case '8':
				c.drawBitmap(BitmapFactory.decodeResource(res, 0x7f020008),  pos + 45 * 4 + 15 * 2, 200, null);
				break;
			case '9':
				c.drawBitmap(BitmapFactory.decodeResource(res, 0x7f020009),  pos + 45 * 4 + 15 * 2, 200, null);
				break;
				
		}
			switch(timz.charAt(3)){
			
			case '0':
				c.drawBitmap(BitmapFactory.decodeResource(res, 0x7f020000),  pos + 45 * 3 + 15, 200, null);
				break;
			case '1':
				c.drawBitmap(BitmapFactory.decodeResource(res, 0x7f020001),  pos + 45 * 3 + 15, 200, null);
				break;
			case '2':
				c.drawBitmap(BitmapFactory.decodeResource(res, 0x7f020002),  pos + 45 * 3 + 15, 200, null);
				break;
			case '3':
				c.drawBitmap(BitmapFactory.decodeResource(res, 0x7f020003),  pos + 45 * 3 + 15, 200, null);
				break;
			case '4':
				c.drawBitmap(BitmapFactory.decodeResource(res, 0x7f020004),  pos + 45 * 3 + 15, 200, null);
				break;
			case '5':
				c.drawBitmap(BitmapFactory.decodeResource(res, 0x7f020005),  pos + 45 * 3 + 15, 200, null);
				break;
			case '6':
				c.drawBitmap(BitmapFactory.decodeResource(res, 0x7f020006),  pos + 45 * 3 + 15, 200, null);
				break;
			case '7':
				c.drawBitmap(BitmapFactory.decodeResource(res, 0x7f020007),  pos + 45 * 3 + 15, 200, null);
				break;
			case '8':
				c.drawBitmap(BitmapFactory.decodeResource(res, 0x7f020008),  pos + 45 * 3 + 15, 200, null);
				break;
			case '9':
				c.drawBitmap(BitmapFactory.decodeResource(res, 0x7f020009),  pos + 45 * 3 + 15, 200, null);
				break;
				
		}
			switch(timz.charAt(2)){
			
			case '0':
				c.drawBitmap(BitmapFactory.decodeResource(res, 0x7f020000),  pos + 45 * 2 + 15, 200, null);
				break;
			case '1':
				c.drawBitmap(BitmapFactory.decodeResource(res, 0x7f020001),  pos + 45 * 2 + 15, 200, null);
				break;
			case '2':
				c.drawBitmap(BitmapFactory.decodeResource(res, 0x7f020002),  pos + 45 * 2 + 15, 200, null);
				break;
			case '3':
				c.drawBitmap(BitmapFactory.decodeResource(res, 0x7f020003),  pos + 45 * 2 + 15, 200, null);
				break;
			case '4':
				c.drawBitmap(BitmapFactory.decodeResource(res, 0x7f020004),  pos + 45 * 2 + 15, 200, null);
				break;
			case '5':
				c.drawBitmap(BitmapFactory.decodeResource(res, 0x7f020005),  pos + 45 * 2 + 15, 200, null);
				break;
			case '6':
				c.drawBitmap(BitmapFactory.decodeResource(res, 0x7f020006),  pos + 45 * 2 + 15, 200, null);
				break;
			case '7':
				c.drawBitmap(BitmapFactory.decodeResource(res, 0x7f020007),  pos + 45 * 2 + 15, 200, null);
				break;
			case '8':
				c.drawBitmap(BitmapFactory.decodeResource(res, 0x7f020008),  pos + 45 * 2 + 15, 200, null);
				break;
			case '9':
				c.drawBitmap(BitmapFactory.decodeResource(res, 0x7f020009),  pos + 45 * 2 + 15, 200, null);
				break;
				
		}
			switch(timz.charAt(4)){
				
				case '0':
					c.drawBitmap(BitmapFactory.decodeResource(res, 0x7f020000),  pos, 200, null);
					break;
				case '1':
					c.drawBitmap(BitmapFactory.decodeResource(res, 0x7f020001),  pos, 200, null);
					break;
				case '2':
					c.drawBitmap(BitmapFactory.decodeResource(res, 0x7f020002),  pos, 200, null);
					break;
				case '3':
					c.drawBitmap(BitmapFactory.decodeResource(res, 0x7f020003),  pos, 200, null);
					break;
				case '4':
					c.drawBitmap(BitmapFactory.decodeResource(res, 0x7f020004),  pos, 200, null);
					break;
				case '5':
					c.drawBitmap(BitmapFactory.decodeResource(res, 0x7f020005),  pos, 200, null);
					break;
				case '6':
					c.drawBitmap(BitmapFactory.decodeResource(res, 0x7f020006),  pos, 200, null);
					break;
				case '7':
					c.drawBitmap(BitmapFactory.decodeResource(res, 0x7f020007),  pos, 200, null);
					break;
				case '8':
					c.drawBitmap(BitmapFactory.decodeResource(res, 0x7f020008),  pos, 200, null);
					break;
				case '9':
					c.drawBitmap(BitmapFactory.decodeResource(res, 0x7f020009),  pos, 200, null);
					break;
					
			}
			
			switch(timz.charAt(5)){
				
				case '0':
					c.drawBitmap(BitmapFactory.decodeResource(res, 0x7f020000),  pos + 45, 200, null);
					break;
				case '1':
					c.drawBitmap(BitmapFactory.decodeResource(res, 0x7f020001),  pos + 45, 200, null);
					break;
				case '2':
					c.drawBitmap(BitmapFactory.decodeResource(res, 0x7f020002),  pos + 45, 200, null);
					break;
				case '3':
					c.drawBitmap(BitmapFactory.decodeResource(res, 0x7f020003),  pos + 45, 200, null);
					break;
				case '4':
					c.drawBitmap(BitmapFactory.decodeResource(res, 0x7f020004),  pos + 45, 200, null);
					break;
				case '5':
					c.drawBitmap(BitmapFactory.decodeResource(res, 0x7f020005),  pos + 45, 200, null);
					break;
				case '6':
					c.drawBitmap(BitmapFactory.decodeResource(res, 0x7f020006),  pos + 45, 200, null);
					break;
				case '7':
					c.drawBitmap(BitmapFactory.decodeResource(res, 0x7f020007),  pos + 45, 200, null);
					break;
				case '8':
					c.drawBitmap(BitmapFactory.decodeResource(res, 0x7f020008),  pos + 45, 200, null);
					break;
				case '9':
					c.drawBitmap(BitmapFactory.decodeResource(res, 0x7f020009),  pos + 45, 200, null);
					break;
					
		}
		}
		
		public void update(){
			i++;
		}
	}

	@Override
	public void onSharedPreferenceChanged(SharedPreferences arg0, String arg1) {
		// TODO Auto-generated method stub
		
	}
	
/*	
	@Override
	public void onLoadResources()
	{
		
//		Set the Base Texture Path
//		Dummie class created as example for GCA
//		=================================
//		this.mBitmapTextureAtlas = new BitmapTextureAtlas(32, 32, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
//      BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
//      this.mParticleTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "particle_fire.png", 0, 0);
//
//      this.mEngine.getTextureManager().loadTexture(this.mBitmapTextureAtlas);
	}

	@Override
	public Scene onLoadScene()
	{
		final Scene scene = new Scene(1);
		scene.setBackground(new ColorBackground(0.0f, 0.0f, 0.0f));

		return scene;
	}

	@Override
	public void onLoadComplete()
	{

	}
	
	@Override
	protected void onTap(final int pX, final int pY)
	{
		
	}
	
	@Override
	public void onSharedPreferenceChanged(SharedPreferences pSharedPrefs, String pKey)
	{
		
	}

	@Override
	public void onPauseGame() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onResumeGame() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onUnloadResources() {
		// TODO Auto-generated method stub
		
	}
*/
	

	// ===========================================================
	// Methods
	// ===========================================================	

	// ===========================================================
	// Inner and Anonymous Classes
	// ===========================================================
}
